package Stereo
{
	import UIFree.uif_bitmap;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class stereoBitmap extends uif_bitmap {
		public function stereoBitmap(a_width: int, a_height: int) {
			super(new BitmapData(a_width, a_height, false, 0xFF000000));
		}
		
		public static function copyChannel(dObject: DisplayObject, channel: uint): Bitmap {
			var tmp: Bitmap = new Bitmap(new BitmapData(dObject.width, dObject.height, true, 0x00000000));
			tmp.bitmapData.draw(dObject);
			var color: uint = (((channel & BitmapDataChannel.RED) > 0)?0xFF0000:0) |
								(((channel & BitmapDataChannel.GREEN) > 0)?0xFF00:0) |
								(((channel & BitmapDataChannel.BLUE) > 0)?0xFF:0);
			
			var copy: Bitmap = new Bitmap(new BitmapData(dObject.width, dObject.height, true, 0xFF000000 | color));
			copy.bitmapData.copyChannel(tmp.bitmapData, tmp.bitmapData.rect, new Point(0, 0), channel, channel);
			copy.bitmapData.copyChannel(tmp.bitmapData, tmp.bitmapData.rect, new Point(0, 0), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
			tmp.bitmapData.dispose();
			copy.smoothing = true;
			return copy;
		}
		
		public function drawFrame(frame: DisplayObject, matrix: Matrix, offset: Number, colorTransform: ColorTransform=null): void {
			var red: Bitmap = copyChannel(frame, BitmapDataChannel.RED);
			var green: Bitmap = copyChannel(frame, BitmapDataChannel.GREEN);
			var blue: Bitmap = copyChannel(frame, BitmapDataChannel.BLUE);
			var mat: Matrix = matrix.clone();
			
			mat.translate(-offset/2, 0);
			bitmapData.draw(red, mat, colorTransform, BlendMode.SCREEN);
			
			mat.translate(offset, 0);
			bitmapData.draw(green, mat, colorTransform, BlendMode.SCREEN);
			bitmapData.draw(blue, mat, colorTransform, BlendMode.SCREEN);
		}			
		
		public static function createMask(dObject: DisplayObject, pos: Point=null, blur: Number=-1, mbright:Number=2): Bitmap {
			var lrect	: Rectangle;
			var tmp		: BitmapData = new BitmapData(dObject.width, dObject.height, true, 0xFF000000);
			tmp.draw(dObject);
			
			var ctc: ColorTransform = new ColorTransform(2, 2, 2, 1, -200 * mbright, -200 * mbright, -200 * mbright);
			
			blur = (blur<0)?tmp.rect.bottomRight.length / 100:blur;
			
			if (blur) tmp.applyFilter(tmp, tmp.rect, new Point(0, 0), new BlurFilter(blur, blur, 3));
			tmp.colorTransform(tmp.rect, ctc);
			var color: uint = 0xFFFF0000;
			
			if (!pos) pos = new Point(tmp.width / 2, tmp.height / 2);
			tmp.floodFill(pos.x, pos.y, color);
			lrect = tmp.getColorBoundsRect(0xFFFFFFFF, color, true);
			lrect.inflate(lrect.width / 40, lrect.height / 40);
			
			var np: Point = new Point(0, 0);
			var sh: Bitmap = new Bitmap(new BitmapData(lrect.width, lrect.height));
			sh.bitmapData.threshold(tmp, lrect, np, '==', color, 0, 0xFFFFFFFF, false);
			sh.bitmapData.applyFilter(sh.bitmapData, sh.bitmapData.rect, np, new BlurFilter(blur, blur, 3));
			
			tmp.dispose();
			sh.x = lrect.x;
			sh.y = lrect.y;
			return sh;
		}
		
		public function photoFrameDraw(photo	: DisplayObject, 
									frame		: DisplayObject, 
									bmask		: Bitmap, 
									pmat		: Matrix, 
									fmat		: Matrix, 
									pct			: ColorTransform,
									frameBase	: Number,
									photoBase	: Number): void {
			var pbmp: Bitmap = new Bitmap(new BitmapData(bmask.width, bmask.height));
			pbmp.bitmapData.draw(photo, pmat);
			pbmp.bitmapData.draw(bmask, null, null, BlendMode.ERASE);
			var mat: Matrix = new Matrix();
			mat.translate(bmask.x, bmask.y);
			mat.concat(fmat);
			
			bitmapData.draw(pbmp, mat, new ColorTransform(0, 0, 0, 1)); // Очищаем подложку под фото
			drawFrame(pbmp, mat, photoBase, pct); // Рисуем фото
			drawFrame(frame, fmat, frameBase);	// Рисуем рамку
		}
	}
}